<?php
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redis;

//文件上传
function upload_file($disk = 'public', $filename, $file)
{
    return Storage::url(Storage::disk($disk)->put($filename, $file));     
}

function test_helper(){
	return "OK";
}

//处理mqtt业务逻辑
//
function on_message($topic, $msg)
{
    //我把数据传递到了另一个方法进行处理  可以在处理完逻辑业务之后 再次调用发布方法  去给订阅方发布消息
    cache(['msg' => $msg]);
}
//管理员状态
function admin_state($state)
{
    $state_arr = [
        1 => '正常',
        2 => '冻结',
    ];
    return $state_arr[$state];
}
//状态标识
function admin_state_class($state)
{
    $state_arr = [
        1 => 'success',
        2 => 'danger',
    ];
    return $state_arr[$state];
}
//设备状态
function machine_state($state)
{
    $state_arr = [
        0 => '未启用',
        1 => '上线',
        2 => '离线',
    ];
    return $state_arr[$state];
}
//设备状态标识
function machine_state_class($state)
{
    $state_arr = [
        0 => 'info',
        1 => 'success',
        2 => 'danger',
    ];
    return $state_arr[$state];
}

//货道状态
function freight_state($state)
{
    $state_arr = [
        '未启用',
        '正常',
        '开路异常',
        '短路异常',
        '检测故障',
        '电路故障',
        '超时故障',
        '货道正忙',
    ];
    return $state_arr[$state];
}
function freight_state_class($state)
{
    $state_arr = [
        0 => 'info',
        1 => 'success',
        2 => 'danger',
        3 => 'danger',
    ];
    return $state_arr[$state];
}
//货道类型
function freight_type($type)
{
    $type_arr = [
        1 => '三线电机',
        2 => '电磁锁',
        3 => '二线电机',
        4 => '带反馈电磁锁',
        5 => '履带',
        6 => '蛇形货道',
        7 => '特殊',
    ];
    return $type_arr[$type];
}

//设备command值
function machine_command($code){
    $command = [
        'state' => 1,
        'setting' => 2,
        'out' => 6,
        'outback' => 5,
        'check' => 4,
        'out_all' => 8,
    ];
    return $command[$code];
}

//商品单位
function goods_unit($code){
    $command = [
        0 => '个',
        1 => '瓶',
        2 => '袋',
        3 => '包',
        4 => '罐',
        5 => '块',
        6 => '盒',
        7 => '提',
        8 => '打',
        9 => '条',
        10 => '箱',
    ];
    return $command[$code];
}

//字符串转分钟$param = hh:mm
function string_to_minute(string $string){
    $arr = explode(':',$string);
    list($hour, $minute) = $arr;
    $minute = (int)$minute + (int)$hour * 60;
    return $minute;
}
//发出出货指令后返回状态码
function machine_out_state($state){
    $stateData = [
        '收到出货指令，立即返回',
        '正常完成',
        '欠流保护失败，未检测到输出电流',
        '过流保护，输出电流过大',
        '转动超时，电动机没有检测到停止开关',
        '货道正忙，上次货道运行中，不能连续输出',
        '货道输出电路故障，过流检测异常',
        'IMEI不符，和本机对应不上',
        '货道类型不符，这版本只能是1，和2',
        '货道编号不符，大于100，或者等于0',
        '出货数量不等于1',
        '订单号已出货',
        '设备重启后，没有建立传输参数成功，不能启动',
        '时间戳过期，服务器发出指令和本机时间对比超出3分钟',
        'json数据异常，解析错误'
    ];
    return $stateData;
}

//字符串加密
function encrypt_str($str)
{
	return Hash::make($str);
}

//状态码提示信息
function code_msg($code)
{
	$returnCode = [
		0    => '成功',
        1001 => '失败',
        2001 => '账户已冻结,请联系客服',
        2002 => '帐号审核',
	];
	return $returnCode[$code];
}
//判断是否为HTTPS
function is_https() {
    if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return 'https://';
    } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
        return 'https://';
    } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        return 'https://';
    }
    return 'http://';
}
/**
 * deleteFile
 * @param string 带路径  到public 层
 * @return 0 1 [返回是否删除成功]
 */
function delete_file($filename){
    $linkpath = public_path().$filename;
    if(is_file($linkpath)){
        if(unlink($linkpath)){
            $re_data = 1;
        }else{
            $re_data = 0;
        }
    }else{
        $re_data = 2;
    }

    return $re_data;
}

/**
 * [list_to_tree description]
 * @param  [type]  $list  [description]
 * @param  string  $pk    [description]
 * @param  string  $pid   [description]
 * @param  string  $child [description]
 * @param  integer $root  [description]
 * @return [type]         [description]
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = 'children',$root=0) {
  // 创建Tree
  $tree = array();
  if(is_array($list)) {
    // 创建基于主键的数组引用
    $refer = array();
    foreach ($list as $key => $data) {
      $refer[$data[$pk]] =& $list[$key];
    }
      foreach ($list as $key => $data) {
      // 判断是否存在parent
      $parentId = $data[$pid];
      if ($root == $parentId) {
        $tree[] =& $list[$key];
      }else{
        if (isset($refer[$parentId])) {
          $parent =& $refer[$parentId];
          $parent[$child][] =& $list[$key];
        }
      }
    }
  }

    return $tree;
}
/**
 * 逆编码获取经纬度
 * $address,$city=''
 */
function get_lat_lng($address){
    /*百度经纬度*/
    //$ak = C('BD_AK');
    //$url = "http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak={$ak}";
    //$res = file_get_contents($url);
    //$data = json_decode($res,true);
    //if($data['status'] == 0){
    //    return $data['result']['location'];
    //}else{
    //    return array('lat'=>'0','lng'=>'0');
    //}
    /*高德经纬度*/
    $address = str_replace(" ","",$address);
    $url = "http://restapi.amap.com/v3/geocode/geo?address=$address&output=json&key=7adec2f6bb05da72030ec2e38d98d858";
    $res = file_get_contents($url);
    $data = json_decode($res,true);
    if($data['status'] == 1){
        $location = explode(",",$data['geocodes'][0]['location']);
        $lng = $location[0];
        $lat = $location[1];
        return array("lat"=>$lat,"lng"=>$lng);
    }else{
        return array('lat'=>'0','lng'=>'0');
    }
}
/*
 * 秒 转化成  20:50:56
 * */
function Sec2Time($times){
    $result = '00:00:00';
    if ($times>0) {
        $hour = floor($times/3600);
        if(intval($hour) <= 9){
            $hour = '0'.$hour;
        }
        $minute = floor(($times-3600 * $hour)/60);
        if(intval($minute) <= 9){
            $minute = '0'.$minute;
        }
        $second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
        if(intval($second) <= 9){
            $second = '0'.$second;
        }
        $result = $hour.':'.$minute.':'.$second;
    }
    return $result;
}

//用户审核状态
function get_user_available($state)
{
    $data = [
        0 => '待审核',
        1 => '通过审核',
        2 => '未通过审核',
    ];

    return $data[$state];
}

//获取服务商哆啦宝参数
function get_dlb_config()
{
    return [
        //代理商编号
        'agent_code' => '10001016014493267623026',
        //公钥
        'accessKey' => '6ebaff9a09554aeb977500d462136652b22a6e17',
        //私钥
        'secretKey' => '23bfadd7aea540f896814e20641f118790a9ded3',
        'store_code' => '',
        'machine_code' => '',
    ];
}


//获取设备mqtt状态
function get_redis_mqtt_state($key, $imei)
{
    $state = json_decode(Redis::get('state:'.$imei), true);
    return $state[$key] ?? '';
}


//温度状态
function tem_state($state){
    $data = ['关闭','制冷','制热'];
    return $data[(int)$state];
}

//照明状态
function led_state($state){
    $data = ['关闭','开启'];
    return $data[(int)$state];
}
//
function mqtt_topic($key, $imei){
    $topics = [
        'setting' => '/topic/get/'.$imei,
        'check' => '/topic/get/'.$imei,
        'out' => '/topic/get/'.$imei,
        'state' => '/topic/State',
        'back_info' => '/topic/put',
        'out_all' => '/topic/get/'.$imei,
        'restart' => '/topic/get/'.$imei,
    ];
    return $topics[$key];
}

//获取默认货
function get_default_freight()
{
    return 100;
}
//前端域名
function get_web_host($imei = '', $freight_way = '')
{
    if($imei != ''){
        $path = is_https().'web.hrcc8.com/index.html?machineId='.$imei;
    }
    if($freight_way != ''){
        $path = is_https().'web.hrcc8.com/index.html?machineId='.$imei.'&freight_way='.$freight_way;
    }
    return $path;
}
//是否过期
function is_expire_time($date)
{
    if(time() >= strtotime($date)){
        return '过期';
    }else{
        return $date;
    }
}

//公告对象类型
function notice_type($type)
{
    $notice_arr = [
        '全部',
        '平台',
        '经销商',
        '商户',
        '运维',
        '其他(除了平台用户)'
    ];
    return $notice_arr[$type];
}


//报单审核
function post_pay($state)
{
    $arr = [
        '未审核', '审核中', '审核失败', '审核完成'
    ];
    return $arr[$state];
}


function payment($type)
{
    $payment_arr = [
       1=> '微信',
       2=> '支付宝'
    ];
    return $payment_arr[$type];
}

function order_status($type)
{
    switch ($type) {
        case 2:
            return '有效订单';
            break;
        
        default:
            return '无效订单';
            break;
    }
}

function refund($type)
{
    $refund_arr = [
        '未退款',
        '等待退款',
        '退款成功'
        
    ];
    return $refund_arr[$type];
}
